import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/views/transfer/logic.dart';
import 'package:seedfour/app/views/withdraw/logic.dart';

import '../../../models/alloc_detail_entity.dart';
import '../../../routes/app_routes.dart';
import '../../../services/api_service.dart';

class FundDetailLogic extends GetxController {
  final apiService = Get.find<ApiService>();

  late ScrollController scrollController;

  var isLoading = true.obs;
  var balance = '0.00'.obs;
  var records = <FundDetailRecord>[].obs;
  var allocDetail = Rxn<AllocDetailAllocContent>();
  var orderInfo = Rxn<AllocDetailOrderInfo>();

  String _id = '';

  @override
  void onInit() {
    super.onInit();

    _id = Get.arguments;

    scrollController = ScrollController();

    reload();
  }

  void reload() {
    isLoading.value = true;
    _getFundDetail();
  }

  void onClickCase(int index) {
    Get.toNamed(RouteGet.caseDetail,
        arguments: orderInfo.value!.invoice!.relateCases![index].caseId);
  }

  /// 点击转账
  void onClickTransfer() async{
    var result = await Get.toNamed(RouteGet.transfer, arguments: TransferParams(balance: balance.value, allocOrderId: _id, isAllocOrder: true));
    if (result != null && result as bool) {
      reload();
    }
  }

  void onClickWithdraw() async {
    // var result = await Get.toNamed(RouteGet.withdraw, arguments: balance.value);
    var result = await Get.toNamed(RouteGet.withdraw, arguments: WithdrawParams(balance: balance.value, allocOrderId: _id, isAllocOrder: true));

    if (result != null && result as bool) {
      reload();
    }
  }

  void _getFundDetail() {
    apiService.getAllocDetail(_id).then((result) {
      isLoading.value = false;
      if (result != null) {
        allocDetail.value = result.allocContent;
        balance.value = '¥${result.allocContent!.remain!.amount}';
        orderInfo.value = result.orderInfo;
        records.clear();
        _plans2Record(result.allocContent!.cplans!);
      }
    });
  }

  void _plans2Record(List<AllocDetailAllocContentCplans> plans) {
    for (var plan in plans) {
      if (plan.plans != null) {
        _subPlans2Record(plan.plans!);
      } else {
        records.add(FundDetailRecord(
          amount: plan.totalAmount ?? 0,
          date: plan.createDate ?? '',
          type: plan.type ?? 999,
          remark: plan.remark ?? '',
        ));
      }
    }
  }

  void _subPlans2Record(List<AllocDetailAllocContentCplansPlans> plans) {
    for (var plan in plans) {
      records.add(FundDetailRecord(
        amount: plan.amount ?? 0,
        date: plan.createDate ?? '',
        type: plan.type ?? 999,
        remark: plan.remark ?? '',
      ));
    }
  }
}

class FundDetailRecord {
  int amount = 0;
  String amountStr = '';
  String date = '';
  int type = 0;
  String typeDesc = '';
  String remark = '';

  FundDetailRecord({
    required this.amount,
    required this.date,
    required this.type,
    required this.remark,
  }) {
    switch (type) {
      case 1:
        typeDesc = '平台消费';
        amountStr = '-¥$amount';
        break;
      case 2:
        typeDesc = '打款';
        amountStr = '-¥$amount';
        break;
      case 3:
        typeDesc = '手工扣款';
        amountStr = '-¥$amount';
        break;
      case 4 || 5:
        typeDesc = '提现';
        amountStr = '-¥$amount';
        break;
      case 6:
        typeDesc = '转出';
        amountStr = '-¥$amount';
        break;
      case 7:
        typeDesc = '转入';
        amountStr = '+¥$amount';
        break;
      case 8:
        typeDesc = '提成转入';
        amountStr = '+¥$amount';
        break;
      case 9:
        typeDesc = '押金解冻';
        amountStr = '+¥$amount';
        break;
      case 10:
        typeDesc = '邀请提成转入';
        amountStr = '+¥$amount';
        break;
      case 11:
        typeDesc = '消费退款转入';
        amountStr = '¥$amount';
        break;
      case 51:
        typeDesc = '提现修改';
        amountStr = '¥$amount';
        break;
      default:
        typeDesc = '收款转入';
        amountStr = '+¥$amount';
        break;
    }
  }
}
